package com.fanghaoran.view.user.viewFrame;

import com.fanghaoran.controller.BrowseController;
import com.fanghaoran.entity.InfoToDisplay;
import com.fanghaoran.view.LoginFrame;


import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: win11
 * @Date: 2023/03/27/21:51
 * @Description:
 */
public class ViewOthersPanel extends JPanel {
    public static final int SEARCH_BY_USERNAME = 0;
    public static final int SEARCH_BY_GENDER = 1;
    public static final int SEARCH_BY_NICKNAME = 2;
    public int searchModel;
    public static JPanel panel;
    private JTable table;

    public ViewOthersPanel() throws Exception {
        panel = this;
        this.setLayout(new BorderLayout());
        //搜索栏
        JPanel searchPanel = new JPanel();
        //设置搜索栏位置
        searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        //添加搜索栏中组件

        //查询条件的选择列表
        List<String> selectList = new ArrayList<>();
        selectList.add("用户名");
        selectList.add("性别");
        selectList.add("昵称");
        Vector<String> vector = new Vector<>(selectList);
        JComboBox<String> selector = new JComboBox<>(vector);
        //设置选择列表显示的行数
        selector.setMaximumRowCount(3);

        //搜索输入框
        JTextField search = new JTextField(30);
        //搜索按钮
        JButton sBtn = new JButton("搜索");
        //排行榜
        JButton rank = new JButton("查看排行榜");
        //不让别人看我信息
        JButton setPrivate=new JButton("不让别人看我信息");


        //选择列表条目点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        if (selector.getSelectedItem().equals(selectList.get(0))) {
                            searchModel = SEARCH_BY_USERNAME;
                        } else if (selector.getSelectedItem().equals(selectList.get(1))) {
                            searchModel = SEARCH_BY_GENDER;
                        } else if (selector.getSelectedItem().equals(selectList.get(2))) {
                            searchModel = SEARCH_BY_NICKNAME;
                        }
                    }
                }
        );

        //按钮点击事件
        sBtn.addActionListener(e -> {
                    List<InfoToDisplay> usersList;
                    if (searchModel == SEARCH_BY_USERNAME) {
                        try {
                            //获取指定搜索条件的用户数据
                            usersList = (List<InfoToDisplay>) new BrowseController().browseByUsername(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new InfoToDisplayTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else if (searchModel == SEARCH_BY_GENDER) {
                        //获取指定搜索条件的用户数据
                        try {
                            usersList = (List<InfoToDisplay>) new BrowseController().browseByGender(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new InfoToDisplayTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else if (searchModel == SEARCH_BY_NICKNAME) {
                        //获取指定搜索条件的用户数据
                        try {
                            usersList = (List<InfoToDisplay>) new BrowseController().browseByNickname(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new InfoToDisplayTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
        );
        //排行榜
        rank.addActionListener(e-> {
                    try {
                        new RankFrame();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }
        );
        //设置私密
        setPrivate.addActionListener(e-> {
                    try {
                        new BrowseController().setPrivate(LoginFrame.usernameInput);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(this,"设置成功");
                }
        );


        searchPanel.add(selector);
        searchPanel.add(search);
        searchPanel.add(sBtn);
        searchPanel.add(rank);
        searchPanel.add(setPrivate);

        //放在panel上面位置
        this.add(searchPanel, BorderLayout.NORTH);

        //展示信息的表格
        //数据库获取所有用户信息
        @SuppressWarnings("unchecked")
        List<InfoToDisplay> usersList = (List<InfoToDisplay>) new BrowseController().getInfoToDisplay().getMessage();
        table = new JTable(new InfoToDisplayTableModel(usersList));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //放在panel中间位置
        this.add(new JScrollPane(table), BorderLayout.CENTER);
    }
}
